/*
** snsPublic
** Author: ZouJL
** Version: 1.4
** Date: 2012.08.30
*/

var snsPublic = window.snsPublic || {};

(function(){

	snsPublic = function(target, options){

		var options = options || {};

		this.target = target;

		this.icon       = options.icon || 'small'; // 分享icon图标大小，默认为'normal'，即大图标，小图标为'small';
		this.showTitle  = options.showTitle || 'true';  // 是否显示'分享到'， 默认显示;
		this.notShareTo = options.notShareTo || null; // 不分享到(如's_copy, s_txwb') 默认为空，表示为分享到所有;
		this.gap        = options.gap || '4px'; // 每个分享左右的间距，如'2px';
		this.offset     = options.offset || [0,0]; // 分享的位置，Top/Left;  
		this.fontSize   = '12px';
		this.title      = options.title || ( document.title ? document.title : '我觉得这个内容很精彩，赶紧分享吧！');
		this.content    = options.content || this.title;
		this.url        = options.url || window.location.href;
		this.pic        = options.pic || '';
		this.monitor    = options.monitor || null;
		this.ats        = options.ats || null; // 新浪和腾讯微博增加@功能
		this.newSNS     = options.newSNS || false; // 区分是否是130325日期后的版本，是的话开心网换为百度贴吧

		this.init();
	}

	snsPublic.prototype = {

		init: function(){
			this.createHtml();
			this.bind_click();
		}
		, sns: {
			s_xlwb    : { bgPos: '-48px',  name: '新浪微博'}
			, s_txwb  : { bgPos: '-168px', name: '腾讯微博'}
			, s_qzone : { bgPos: '-8px',   name: 'QQ空间'}
			, s_renren: { bgPos: '-128px', name: '人人网'}
			, s_kaixin: { bgPos: '-208px', name: '开心网'}
			, s_tieba : { bgPos: '-528px', name: '百度贴吧'}
			/*, s_baidu : { bgPos: '-88px',  name: '百度搜藏'}
			, s_shwb  : { bgPos: '', name: '搜狐微博'}
			, s_bsh   : { bgPos: '', name: '白社会'}
			, s_fetion: { bgPos: '', name: '飞信'}
			, s_douban: { bgPos: '', name: '豆瓣'}*/
		}
		, _sns: 's_kaixin' // 新版分享，需要被替换的
		, __sns: 's_tieba' // 新版分享，被替换后的
		, style: function(){
			var that       = this
				, fontSize = '12px'
				, height   = '16px'
				, width    = '20px'
				, bgImg    = 'http://i1.cy.com/www/public/share/share_16.png'
				// , bgImg    = 'images/icon_16.png'
				;

			if( that.icon == 'normal' ){
				fontSize  = '14px'
				, height  = '32px'
				, width   = '36px'
				, bgImg   = 'http://i1.cy.com/www/public/share/share_32.png'
				// , bgImg   = 'images/icon_32.png'
				;	
			}

			return {
				box    : 'font-size:12px;overflow:hidden;_height:1%;width:auto;position:absolute;top:' + that.offset[0] + ';left:' + that.offset[1] + ';'
				, aSpan: 'font-size:' + fontSize + ';height:' + height + ';line-height:' + height + ';display:inline-block;*display:inline;*zoom:1;float:left;margin-right:' + that.gap + ';'
				, a    : 'width:' + width + ';text-indent:-9999em;cursor:pointer;background:url(' + bgImg + ') no-repeat;'
			}
		}
		, createHtml: function(target){

			var that       = this
				, target   = that.target ? document.getElementById(that.target) : document.body
				, sns      = that.sns
				, _sns     = that._sns
				, __sns     = that.__sns
				, newSNS   = that.newSNS
				, notShare = that.notShareTo
				, css      = that.style()
				, html     = that.showTitle=='true' ? '<span style="' + css.aSpan + '">分享到：</span>' : '';

			for( key in sns ){
				if( newSNS ){ // 区分是否是130325日期后的版本，是的话开心网换为百度贴吧
					if( !(notShare && notShare.indexOf(key)!=-1) && !(_sns && _sns.indexOf(key)!=-1) ){
						html += '<a class="' + key + '" style="' + css.aSpan + css.a + 'background-position:0 ' + sns[key].bgPos + ';" title="分享到' + sns[key].name + '">' + sns[key].name + '</a>';
					}
				}else if( !(notShare && notShare.indexOf(key)!=-1) && !(__sns && __sns.indexOf(key)!=-1) && key != 's_xlwb' ){
					html += '<a class="' + key + '" style="' + css.aSpan + css.a + 'background-position:0 ' + sns[key].bgPos + ';" title="分享到' + sns[key].name + '">' + sns[key].name + '</a>';
				} else if(!(notShare && notShare.indexOf(key)!=-1) && !(__sns && __sns.indexOf(key)!=-1) && key == 's_xlwb'){
					html += '<div class="dddd" style="' + css.aSpan + 'height:24px;padding:0 0 0 4px;width:20px;margin:-3px 0 0 0;' + ';"><wb:share-button appkey="3leGxl" addition="simple" type="icon" ralateUid="2501896357"></wb:share-button></div>'	
				}
			}

			var parent = document.createElement('div');
			parent.className = 'tr_share';
			parent.setAttribute('style', css.box);
			parent.innerHTML = html;

			target.appendChild(parent);
		}
		, bind_click: function(){

			var that     = this
				, target = that.target ? document.getElementById(that.target) : document.body
				, links  = target.getElementsByTagName('a')
				, fun    = that.fun();

			for( var i=0, len=links.length; i<len; i++ ){
				links[i].onclick = (function(a){
					var cl = links[a].className;
					return function(){
						fun[cl](cl);						
					}
			    })(i);	
			}

		}
		, fun: function(){
			
			var that      = this
				, url     = encodeURIComponent(that.url)
				, title   = encodeURIComponent(that.title)
				, content = encodeURIComponent(that.content)
				, pic     = encodeURIComponent(that.pic)
				, monitor = that.monitor ? that.monitor : {}
				, ats     = that.ats ? that.ats : {};

			return {
				s_xlwb  : function(key){
					var u  = monitor[key] ? monitor[key] : '';
					var at = encodeURIComponent( ats[key] ? ( ' ' + ats[key] + ' ' ) : '' );
					u += 'http://v.t.sina.com.cn/share/share.php?c=&url=' + url + '&title=' + content + at + '&pic=' + pic;		
					window.open(u, 'sina');
					void(0);
				}
				, s_txwb  : function(key){
					var u = monitor[key] ? monitor[key] : '';
					var at = encodeURIComponent( ats[key] ? ( ' ' + ats[key] + ' ' ) : '' );
					u += 'http://share.v.t.qq.com/index.php?c=share&a=index'+ '&url=' + url + '&pic=' + pic + '&title=' + content + at;
					window.open(u, 'qqmb');
				}
				, s_qzone : function(key){
					var u = monitor[key] ? monitor[key] : '';
					u += 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + url + '&title=' + title + '&desc=&site=&summary=' + content;
					window.open(u, 'qzone');
					void(0);
				}
				, s_renren: function(key){
					var u = monitor[key] ? monitor[key] : '';
					u += 'http://widget.renren.com/dialog/share?resourceUrl=' + url + '&srcUrl=' + url + '&title=' + title + '&pic=' + pic + '&description=' + content;
					window.open(u, 'renren');
					void(0);
				}
				, s_kaixin: function(key){
					var u = monitor[key] ? monitor[key] : '';
					// u += 'http://www.kaixin001.com/repaste/share.php?rurl=' + url + '&rtitle=' + title + '&rcontent=' + content;
					u += 'http://www.kaixin001.com/repaste/share.php?rurl=' + url + '&rcontent=' + content;
					window.open(u, 'kaixin');
					void(0);
				}
				, s_baidu : function(key){
					var u = monitor[key] ? monitor[key] : '';
					u += 'http://cang.baidu.com/do/add?it=' + title +'&iu=' + url + '&dc=' + content + '&tn=' + encodeURIComponent('网游');
					window.open(u, 'baidu');
					void(0);
				}
				, s_shwb  : function(key){
					var u = monitor[key] ? monitor[key] : '';
					u += 'http://t.sohu.com/third/post.jsp?&url=' + url + '&title=' + title + '&content=utf-8';
					window.open(u, 'sohu');
					void(0);
				}
				, s_bsh   : function(){
					var u = monitor[key] ? monitor[key] : '';
					u += 'http://bai.sohu.com/share/blank/addbutton.do?from=tudou&link=' + url + '&title=' + title + '&reason=' + content + '&picurls=' + pic;
					window.open(u, 'baish');
					void(0);
				}
				, s_fetion: function(){
					var u = monitor[key] ? monitor[key] : '';
					u += 'http://space.feixin.10086.cn/api/share?source=搜狐畅游&title=' + title + '&url=' + url;
					window.open(u, 'fetion');
					void(0);
				}
				, s_douban: function(){
					var u = monitor[key] ? monitor[key] : '';
					u += 'http://www.douban.com/recommend/?url=' + url + '&title=' + title + '&comment=' + content;
					window.open(u, 'douban');
					void(0);
				}
				, s_tieba: function(){
					var u = monitor[key] ? monitor[key] : '';
					u += 'http://tieba.baidu.com/f/commit/share/openShareApi?url=' + url + '&title=' + title + '&comment=' + content;
					window.open(u, 'tieba');
					void(0);
				}
			}
		}

	};

})();